package com.example.system01.service;

import com.example.system01.dao.UserDao;
import com.example.system01.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserService implements UserDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int add(User user) {
        String sql = "insert into user values(null,'"+user.getUsername()
                +"','"+user.getPassword()+"','0','0')";
        return jdbcTemplate.update(sql);
    }

    @Override
    public int delete(User user) {
        return 0;
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public User findOne(User user) {
        String sql = "select * from user where username like '%"+user.getUsername()+"%'";
        System.out.println(sql);
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class));
//        User userdb = new User();
//        userdb.setUsername("admin");
//        userdb.setPassword("admin123");
////        用传递过来的user对象和数据库里的用户信息比较
//        String username = user.getUsername();
//        String password = user.getPassword();
//        if(username.equals(userdb.getUsername())){
//            if (password.equals(userdb.getPassword())){
//                return userdb;
//            }
//            else {
//                return null;
//            }
//        }
//        else {
//            return null;
//        }
    }

    @Override
    public List<User> findAll(User user) {
        String sql = "select * from user";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
    }

}
